﻿using Microsoft.Extensions.DependencyInjection;
using System;
using yiwei.Repository.Interface;
using yiwei.Repository;
using Microsoft.EntityFrameworkCore;
using yiwei.Repository.Entity;
using System.Collections.Generic;
using System.Threading;
using Microsoft.Extensions.Logging;

namespace yiwei.UnitTestProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            //注入
            IServiceCollection services = new ServiceCollection();
            //services.AddTransient<IPayService, AliPayService>();
            //
          
            string conn = "Data Source=127.0.0.1;Initial Catalog=YiWeiDBPro;User=sa;Password=123456";
            services.AddDbContext<YiWeiDBContext>(options =>
             {
                 options.UseSqlServer(conn)
                 .UseLoggerFactory(new LoggerFactory(new[] { new EFLoggerProvider() }));//添加日志监测
             });
            services.AddScoped<IRepository, SqlRepository > ();
            services.AddScoped<DbContext, YiWeiDBContext>();
            var provider = services.BuildServiceProvider();
            //var TesContext = provider.GetService<YiWeiDBContext>();
            //User u1 = new User();
            //u1.Account = "123";
            //u1.Password = "123";
            //u1.Name = "yiwei";
            //TesContext.Users.Add(u1);
            //TesContext.SaveChanges();
            // services.AddSingleton <IRepository>(new SqlRepository(provider.GetService<YiWeiDBContext>()));


            //数据库使用
            //using (var db = provider.GetService<IRepository>())
            var db = provider.GetService<IRepository>();

            //{
            List<User> Users = new List<User>();
                for (int x = 0; x < 1000; x++)
                {
                    User u = new User();
                    u.Account = "user-1231"+x.ToString();
                    u.Password = "123";
                    u.Name = "yiwei";
                    u.Remark = "Test";                 
                    Users.Add(u);

                }
            //int requertInt = db.Insert<User>(Users);
            var uses = await db.FindListAsync<User>();
            //var requeInt = db.DeleteAsync<User>(uses);
           
            foreach (var t in  uses)
            {
                t.Remark = "update";
            }

            //db.Update<User>(uses);
            //Thread.Sleep(1000);
            //}

        }
    }


    public class EFLoggerProvider : ILoggerProvider
    {
        public ILogger CreateLogger(string categoryName) => new EFLogger(categoryName);
        public void Dispose() { }
    }
    /// <summary>
    /// EFLogger
    /// </summary>
    public class EFLogger : ILogger
    {
        private readonly string categoryName;
        public EFLogger(string categoryName) => this.categoryName = categoryName;
        public bool IsEnabled(LogLevel logLevel) => true;
        public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
        {
            //ef core执行数据库查询时的categoryName为Microsoft.EntityFrameworkCore.Database.Command,日志级别为Information
            if (categoryName == "Microsoft.EntityFrameworkCore.Database.Command" && logLevel == LogLevel.Information)
            {
                var logContent = formatter(state, exception);
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine(logContent);
                Console.ResetColor();
            }
        }
        public IDisposable BeginScope<TState>(TState state) => null;
    }
}
